package org.snail.department.controller;

import com.alibaba.fastjson.JSON;
import com.dingtalk.open.client.api.model.corp.CorpUserDetail;
import com.dingtalk.open.client.api.model.corp.Department;
import lombok.extern.log4j.Log4j;
import org.snail.common.dingding.auth.AuthHelper;
import org.snail.common.dingding.department.DepartmentHelper;
import org.snail.common.dingding.user.UserHelper;
import org.snail.dept.bean.DepartDO;
import org.snail.dept.bean.TreeNode;
import org.snail.department.service.impl.DepartmentTreeNodeService;
import org.snail.employee.service.impl.EmployeesServiceImpl;
import org.snail.exception.CommonException;
import org.snail.exception.JsonResult;
import org.snail.exception.SqlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("department")
@Log4j
public class DepartmentController {

    @Autowired
    private DepartmentTreeNodeService departmentTreeNodeService;

    @Autowired
    private EmployeesServiceImpl employeesService;


    @RequestMapping(value = "getTreeNode", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getTreeNode() throws SqlException, CommonException {
            TreeNode treeNode = (TreeNode) departmentTreeNodeService.getTreeNode();
        return JSON.toJSONString(treeNode);
    }

    @RequestMapping("delTreeNode")
    @ResponseBody
    public void delTreeNode() {
        departmentTreeNodeService.delTreeNode();
    }

    /**
     * 搜索栏提示展示  喂喂喂
     * @param deptName
     * @return
     */
    @RequestMapping("exhibitDeptInfo")
    @ResponseBody
    public List<DepartDO> exhibitDeptInfo(String deptName) {
        return departmentTreeNodeService.getDeptInfoByName(deptName);
    }

    @RequestMapping("test")
    @ResponseBody
    public void exhibitDeptInfo() {
        Map<Object, Object> objectObjectMap = departmentTreeNodeService.selectDeptByEmployeeId();
    }

    @RequestMapping("findEmpByDingding")
    @ResponseBody
    public JsonResult findEmpByDingding(){
        JsonResult jsonResult = new JsonResult();
        List<String> empFromDingding = new ArrayList<>();
        try {
            List<Department> departmentList = DepartmentHelper.listDepartments(AuthHelper.getAccessToken(), 25393462 + "");
            for(Department department:departmentList){
                List<CorpUserDetail> userDetailList = UserHelper.getAllUserDetails(AuthHelper.getAccessToken(),department.getId(), null);
                for(CorpUserDetail corpUserDetail:userDetailList){
                    empFromDingding.add(corpUserDetail.getName());
                }
            }
        } catch (Exception e) {

        }
        jsonResult.setData(empFromDingding);
        return jsonResult;
    }
}